﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Exceptions.RepositoryExceptions;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.ExternalModels.FilterModels;

namespace SQLRepositories.FilterRepositories
{
    public class IndustryFilterRepository : EntityBaseRepository<IndustryFilterModel>, IFilterRepository<IndustryFilterModel>
    {
        public IndustryFilterRepository(IDBEconomicInformationServiceContext context) 
            : base(context)
        {}

        public void Create(IndustryFilterModel item)
        {
            Context.IndustryFilters.Add(item);
            Context.SaveChanges();
        }

        public IndustryFilterModel GetById(Guid id)
        {
            return Context.IndustryFilters.FirstOrDefault(x => x.Id.Equals(id));
        }

        public void Delete(Guid id)
        {
            var removedItem = Context.IndustryFilters.FirstOrDefault(x => x.Id.Equals(id));
            if(removedItem == null)
                throw new EntityNotFoundException("IndustryFilter");
            Context.IndustryFilters.Remove(removedItem);
            Context.SaveChanges();
        }

        public void Delete(IndustryFilterModel item)
        {
            Context.IndustryFilters.Remove(item);
            Context.SaveChanges();
        }

        public IEnumerable<IndustryFilterModel> GetAll()
        {
            return Context.IndustryFilters;
        }
    }
}
